package util;

public final class MyParser {

	// public static String LINE ="L";
	// public static String SQUARE ="SQ";
	// public static String TRIANGLE ="T";
	// public static String CIRCLE ="C";
	// public static String COLOR = "CO";

	public static String DATA = "<<";
	public static String FIELD = ";;";
	public static String GAME = "G";
	public static String CHAT = "C";
	public static String SERVER = "S";
	public static String PAINT = "P";

	public static String POINT = "::";
	public static String COORDINATE = "@@";

	public static String pointToString(Point p) {
		return (p.getX() + POINT + p.getY());
	}

	public static String listToString(PointList p) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < p.size(); i++) {
			sb.append(pointToString(p.get(i)) + COORDINATE);
		}
		return sb.toString();
	}

	public static Point stringToPoint(String s) {
		String[] sv = s.split(POINT);
		if (sv.length == 2) {
			return new Point(Integer.parseInt(sv[0]), Integer.parseInt(sv[1]));
		}
		return null;
	}

	public static PointList stringToList(String s) {
		String[] sv = s.split(COORDINATE);
		PointList pl = new PointList();
		for (int i = 0; i < sv.length; i++) {
			pl.add(stringToPoint(sv[i]));
		}
		return pl;
	}

	public static boolean isGame(String s) {
		String[] temp = s.split(FIELD);
		if (temp != null) {
			if (temp.length > 1) {
				return temp[1].equals(GAME);
			}
		}
		return false;
	}

	public static boolean isChat(String s) {
		String[] temp = s.split(FIELD);
		if (temp != null) {
			if (temp.length > 1) {
				return temp[1].equals(CHAT);
			}
		}
		return false;
	}

	public static boolean isServerMessage(String s) {
		String[] temp = s.split(FIELD);
		if (temp != null) {
			if (temp.length > 1) {
				return temp[1].equals(SERVER);
			}
		}
		return false;
	}

	public static boolean isPaint(String s) {
		if (s != null) {
			String[] temp = s.split(FIELD);
			if (temp != null) {
				if (temp.length > 1) {
					return temp[1].equals(PAINT);
				}
			}
		}
		return false;
	}

	public static String getName(String s) {
		if (s != null) {

			String[] temp = s.split(FIELD);
			if (temp != null) {
				return temp[0];
			}
		}
		return null;
	}

	public static String indexToName(String s, String name) {
		if (s != null && name != null) {

			String[] temp = s.split(FIELD);
			if (temp != null) {
				temp[0] = name;
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < temp.length; i++) {
					sb.append(temp[i] + FIELD);
				}
				return sb.toString();
			}
		}
		return null;
	}

	public static String concateFields(String s1, String s2) {
		return s1 + FIELD + s2;
	}

	public static String codeToChat(String s, String name) {
		return name + FIELD + CHAT + FIELD + s;
	}

	public static String codeToGame(String s) {
		return "Server" + MyParser.FIELD + MyParser.GAME + MyParser.FIELD + s;
	}

	public static String codeToChatClient(String s) {
		if (s != null) {

			String[] temp = s.split(FIELD);
			return temp[0] + ": " + temp[2];

		}
		return null;
	}

	public static String getMessage(String s) {
		if (s != null) {

			String[] temp = s.split(FIELD);
			if (temp.length >= 3) {
				return temp[2];
			}
		}
		return null;
	}

	public static String[] getData(String input) {
		if (input != null) {

			String[] sField = input.split(FIELD);
			if (sField.length >= 4) {
				String[] sData = sField[3].split(DATA);

				return sData;
			}
		}
		return null;
	}
}
